#include "HttpResponse.h"

HttpResponse::HttpResponse(int statu)
    : _statu(statu),
      _redirect_flag(false)
{
}

// 1. 重置状态
void HttpResponse::HttpResponse::ReSet()
{
    _statu = 200;
    _redirect_flag = false;
    _body.clear();
    _redirect_url.clear();
    _headers.clear();
}

// 2. 插入头部字段
void HttpResponse::SetHeader(const std::string &key, const std::string &val)
{
    _headers.insert({key, val});
}

// 3. 判断是否存在指定头部字段
bool HttpResponse::HasHeader(const std::string &key)
{
    auto it = _headers.find(key);
    if (it == _headers.end())
        return false;
    return true;
}

// 4. 获取头部字段
std::string HttpResponse::GetHeader(const std::string &key)
{
    auto it = _headers.find(key);
    if (it == _headers.end())
        return "";
    return it->second;
}

// 5. 设置头部字段
void HttpResponse::SetContent(const std::string &body, const std::string &type)
{
    _body = body;
    SetHeader("Content-Type", type);
}

// 6. 设置重定向路径
void HttpResponse::SetRedirect(const std::string &url, int statu)
{
    _statu = statu;
    _redirect_flag = true;
    _redirect_url = url;
}

// 7. 判断是否是短连接
bool HttpResponse::Close()
{
    if (GetHeader("Connection") == "keep-alive")
        return false;
    return true;
}